import { func } from "prop-types";

export class LightPrompt {
    constructor() {
        this.state = {
            parentDom: null,
            promptDom: null,
            contentDom: null,
            strDom: null,
            iconDom: null
        }

    }
    //创建dom节点
    createDom = (obj) => {
        this.state.parentDom = document.getElementById(obj.domId);
        this.state.promptDom = document.createElement('div');
        this.state.contentDom = document.createElement('div');
        this.state.strDom = document.createElement('div')
        this.state.iconDom = document.createElement('div')

        this.state.iconDom.className = "iconfont icon-jiazai_dan iconDom"
        this.state.promptDom.className = 'promptDom'
        this.state.contentDom.className = "contentDom"
        this.state.strDom.className = "strDom"

        this.state.strDom.innerHTML = obj.str
        this.state.contentDom.appendChild(this.state.strDom)
        this.state.contentDom.appendChild(this.state.iconDom)
        this.state.promptDom.appendChild(this.state.contentDom)
        this.domClick(obj)
    }
    //是否添加点击事件
    domClick = (obj) => {
        console.log()
        if (typeof obj.click === 'boolean') {
            if (obj.click === true) {
                this.state.promptDom.onclick = () => {
                    this.state.promptDom.remove()
                }
            } else {
                return
            }
        } else {
            console.error('[ERROR]:Click event parameter type should be boolean.')
        }
        // this.state.promptDom.onclick=()=>{
        //     this.state.promptDom.remove()
        // }
    }
    // 加载弹窗
    loading = (obj) => {
        this.createDom(obj)
        let sonList = [...this.state.parentDom.children]
        sonList.forEach(item => {
            if (item.className !== 'promptDom') {

                this.state.parentDom.appendChild(this.state.promptDom)
                console.log('创建')
                return
            } else {
                this.state.parentDom.removeChild(item)
                console.log('删除')
                this.state.parentDom.appendChild(this.state.promptDom)
                console.log('再创建')
            }
        })

        this.createdomStyle(this.state.promptDom)
        this.clearDom(obj)
    }
    // 成功的弹窗
    success = (obj) => {
        this.createDom(obj)
        let sonList = [...this.state.parentDom.children]
        sonList.forEach(item => {
            if (item.className !== 'promptDom') {

                this.state.parentDom.appendChild(this.state.promptDom)
                console.log('创建')
                return
            } else {
                this.state.parentDom.removeChild(item)
                console.log('删除')
                this.state.parentDom.appendChild(this.state.promptDom)
                console.log('再创建')

            }
        })

        this.createdomStyle(this.state.promptDom)
        this.clearDom(obj)
    }
    //提示弹窗！！！！！
    warning = (obj) => {
        this.createDom(obj);
        this.state.iconDom.className = "iconfont icon-gantanhao_fuzhi";
        let sonList = [...this.state.parentDom.children]
        sonList.forEach(item => {
            if (item.className !== 'promptDom') {

                this.state.parentDom.appendChild(this.state.promptDom)
                console.log('创建')
                return
            } else {
                this.state.parentDom.removeChild(item)
                console.log('删除')
                this.state.parentDom.appendChild(this.state.promptDom)
                console.log('再创建')
            }
        })
        this.createdomStyle(this.state.promptDom)
        this.clearDom(obj)
    }
    //弹出过渡
    createdomStyle = (contentDom) => {
        setTimeout(() => {
            contentDom.style.opacity = '1'
        }, 300);
    }
    //清除节点
    clearDom = (obj) => {

        setTimeout(() => {
            this.state.promptDom.remove()
        }, obj.cleartime);
    }
}
